<template>
  <div class="white-box">
    <div class="table-mb">
      <el-button type="primary" @click="handleAdd()" :icon="CirclePlus">添加自提点</el-button>
    </div>
    <div class="task-one">
      <div class="task-one__item">
        <span class="task-one__label">门店名称：</span>
        <el-input placeholder="请输入门店名称" v-model="searchParams.storesName" @keyup.enter.native="handleSearch"
                  clearable></el-input>
      </div>
      <div class="task-one__item">
        <span class="task-one__label">省/市/区：</span>
        <elui-china-area-dht v-model="searchRegion" @change="handleSearchRegionChange" clearable></elui-china-area-dht>
      </div>
      <div class="task-one__item">
        <span class="task-one__label">联系人：</span>
        <el-input placeholder="请输入联系人" v-model="searchParams.name" @keyup.enter.native="handleSearch"
                  clearable></el-input>
      </div>
      <div class="task-one__item">
        <span class="task-one__label">联系电话：</span>
        <el-input placeholder="请输入联系电话" v-model="searchParams.phone" @keyup.enter.native="handleSearch"
                  clearable></el-input>
      </div>
      <div class="task-one__item">
        <span class="task-one__label">详细地址：</span>
        <el-input placeholder="请输入详细地址" v-model="searchParams.address" @keyup.enter.native="handleSearch"
                  clearable></el-input>
      </div>
      <div class="task-one__btn task-wrap-btn clearfix">
        <el-button type="primary" :loading="searchBtnLoading" :icon="Search" @click="handleSearch">搜索</el-button>
      </div>
    </div>

    <el-table class="table-mb minArea-table" v-loading="loading" :data="list" :header-cell-style="{ background: '#F5F7FA' }"
              element-loading-background="rgba(255, 255, 255, 0.6)">
      <el-table-column prop="storesImg" label="门店图片" width="120">
        <template #default="{ row }">
          <el-image
              v-if="row.storesImg"
              style="width: 80px; height: 80px"
              :src="proxy.imgUrl + row.storesImg"
              :zoom-rate="1.2"
              :max-scale="7"
              :min-scale="0.2"
              :preview-src-list="[proxy.imgUrl + row.storesImg]"
              :initial-index="0"
              :preview-teleported="true"
              :teleported="true"
              fit="cover"
          />
        </template>
      </el-table-column>
      <el-table-column prop="storesName" label="门店名称"> </el-table-column>
      <el-table-column prop="name" label="联系人"> </el-table-column>
      <el-table-column prop="phone" label="联系电话"> </el-table-column>
      <el-table-column prop="addr" label="地址" min-width="120">
        <template #default="{ row }">
          <div>{{row.province}}/{{row.city}}/{{row.district}}</div>
          <div>{{row.address}}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="status" label="状态"  width="100">
        <template #default="{ row }">
          <el-text v-if="row.status === 0" type="success">正常</el-text>
          <el-text v-else-if="row.status === 1" type="info">停用</el-text>
          <el-text v-else type="info"></el-text>
        </template>
      </el-table-column>
      <el-table-column prop="created" label="创建时间"> </el-table-column>
      <el-table-column label="操作" width="180" fixed="right">
        <template #default="{ row }">
          <el-button link type="primary" @click="handleEdit(row,{sid:row.sid})">编辑</el-button>
          <el-popconfirm :persistent="false" title="确定删除吗？" confirm-button-text="确定" cancel-button-text="取消"
                         @confirm="handleDelete(row.sid)">
            <template #reference>
              <el-button link size="small" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination background v-model:current-page="page" v-model:page-size="pageSize" :page-sizes="pageSizeNum"
                   :total="totalNums" layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>


    <el-dialog v-model="dialogShow" :title="dialogParams.type == 'add'?'添加':'编辑'" width="760px" :append-to-body="true" :destroy-on-close="true"
               :close-on-click-modal="false" @close="closeDialog">
      <el-form ref="dialogFormRef" :model="dialogForm" :rules="dialogFormRules" label-width="120px" style="width: 660px;">

        <el-form-item label="门店名称" prop="storesName">
          <el-input v-model.trim="dialogForm.storesName" clearable placeholder="请输入门店名称	"/>
        </el-form-item>
        <el-form-item label="区域" prop="region">
          <elui-china-area-dht v-model="dialogForm.region" @change="handleRegionChange" style="width: 100%"></elui-china-area-dht>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model.trim="dialogForm.address" clearable placeholder="请输入详细地址"  :rows="2" type="textarea"/>
        </el-form-item>
        <el-form-item label="门店图片" prop="storesImg">
          <File-upload ref="storesImgRef" v-model="dialogForm.storesImg" :fileSize="50" :limit="1" modelValueType="String" :fileType="['png', 'jpg', 'jpeg']" listType="picture-card" img-size="120px*120px"></File-upload>
        </el-form-item>
        <el-form-item label="联系人" prop="name">
          <el-input v-model.trim="dialogForm.name" clearable placeholder="请输入联系人	"/>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model.trim="dialogForm.phone" clearable placeholder="请输入联系电话	"/>
        </el-form-item>


        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="dialogForm.status">
            <el-radio :value="0">正常</el-radio>
            <el-radio :value="1">停用</el-radio>
          </el-radio-group>
        </el-form-item>



        <el-form-item>
          <div class="flexs">
            <el-button type="primary" :loading="dialogParams.loading" @click="submitDialogForm2(dialogFormRef)">
              确认
            </el-button>
            <el-button @click="resetDialogForm(dialogFormRef)">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>

    <go-back/>
  </div>
</template>

<script setup>
import {
  getCurrentInstance,
  ref,
  reactive,
  onMounted,
  watch, computed
} from "vue"
import listMixins from "@mixins/hook/list"
import {
  useRouter
} from "vue-router"
import {
  CirclePlus,
  Search
} from "@element-plus/icons-vue"
import GoBack from "@components/GoBack.vue"
import {EluiChinaAreaDht} from "elui-china-area-dht";
import chinaAreaData from "china-area-data";
import FileUpload from "@components/FileUpload.vue";

const router = useRouter()
const {
  proxy
} = getCurrentInstance()

/**
 * minxin列表加载
 */
const {
  page,
  pageSizeNum,
  pageSize,
  loading,
  totalNums,
  searchParams,
  searchBtnLoading,
  apiPath,
  listApiPath,
  list,
  dialogFormRef,
  dialogShow,
  dialogForm,
  dialogParams,
  handleSearch,
  getData,
  handleAdd,
  // handleEdit,
  resetDialogForm,
  handleAllSelected,
  submitDialogForm,
  closeDialog,
  handleAction,
  handleDelete
} = listMixins()

dialogForm.address = '';
dialogForm.city = '';
dialogForm.cityCode = '';
dialogForm.district = '';
dialogForm.districtCode = '';
dialogForm.name = '';
dialogForm.phone = '';
dialogForm.province = '';
dialogForm.provinceCode = '';
dialogForm.storesImg = '';
dialogForm.storesName = '';
dialogForm.status = 0;


apiPath.value = '/pickup/address';

const searchRegion = reactive([])
const handleSearchRegionChange =(e)=>{
  if(!e){
    searchParams.province = ''
    searchParams.city = ''
    searchParams.district = ''

  }else{
    searchParams.province = chinaAreaData['86'][e[0]]
    searchParams.city = chinaAreaData[e[0]][e[1]]
    searchParams.district = chinaAreaData[e[1]][e[2]]
  }
}

onMounted(() => {
  getData()
})

const dialogFormRules = reactive({
  storesName:[{
    required: true,
    message: "请输入门店名称",
    trigger: "blur"
  }],
  region: [{
    required: true,
    message: "请选择省市区",
    trigger: "change"
  }],
  address:[{
    required: true,
    message: "请输入详细地址",
    trigger: "blur"
  }],
})
const handleEdit = (row,data) => {
  dialogShow.value = true
  dialogParams.type = 'edit'
  dialogParams.data = data
  setTimeout(() => {
    for (let key in dialogForm) {
      dialogForm[key] = row[key]
    }
    dialogForm.region = [];
    dialogForm.region.push(row.provinceCode.toString())
    dialogForm.region.push(row.cityCode.toString())
    dialogForm.region.push(row.districtCode.toString())
  }, 50)
}
const handleRegionChange = (e) => {
  dialogForm.province = chinaAreaData['86'][e[0]]
  dialogForm.provinceCode = e[0]
  dialogForm.city = chinaAreaData[e[0]][e[1]]
  dialogForm.cityCode = e[1]
  dialogForm.district = chinaAreaData[e[1]][e[2]]
  dialogForm.districtCode = e[2]
}
const submitDialogForm2 = (formEl) => {
  let params = JSON.parse(JSON.stringify(dialogForm));
  delete params.region
  submitDialogForm(formEl, params)
}


const selectOptions = reactive({
  house_type_id:[],
  pianqu_id:[]
})

const handleLon = ()=> {
  window.open("https://lbs.qq.com/getPoint/");
}

</script>

<style lang="less">
.minArea-table{
  .el-descriptions__cell {
    padding-bottom: 0 !important;
  }

  .el-descriptions__label {
    color: #999999 !important
  }
  .el-descriptions__table tr,.el-descriptions__body{
    background: none !important;
  }
}

.task-one .el-cascader .el-input{
  width: 300px;
}

</style>